package cc.miwuya.complain.controller;

import cc.miwuya.common.util.JwtUtils;
import cc.miwuya.common.util.Result;
import cc.miwuya.complain.domain.Complain;
import cc.miwuya.complain.service.ComplainService;
import cc.miwuya.core.BaseController;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author 迷雾森林
 * @Date 2024-02-24 10:29
 * 投诉控制器
 */
@RestController
@RequestMapping("/common/complain")
public class ComplainController extends BaseController {

    @Resource
    ComplainService complainService;
    @Resource
    HttpServletRequest httpServletRequest;
    /**
     * 获取投诉列表
     */
    @GetMapping("/list")
    public Result list(String name,Long residentialId){
        for (Integer i : getRole()) {
            System.out.println(i);
        }
        Object page = complainService.getPage(getPage(), name, residentialId,getRole());
        return Result.success(page);
    }

    /**
     * 发表或回复投诉
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Complain complain){
        if (!StrUtil.isEmpty(complain.getReply())){
            complain.setOperatorId(getCurrent());
            complain.setReplyTime(LocalDateTime.now());
        }
        complain.setUpdated(LocalDateTime.now());
        complainService.saveOrUpdate(complain);
        return Result.success();
    }

    /**
     * 根据ID删除投诉
     */
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        complainService.removeById(id);
        return Result.success();
    }

    /**
     * 根据ID获取投诉详情
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable Long id){
        Complain complain = complainService.getById(id);
        return Result.success(complain);
    }
    public Long getCurrent(){
        String authorization = httpServletRequest.getHeader("Authorization");
        Claims claim = JwtUtils.getClaimByToken(authorization, "community");
        JSON parse = JSONUtil.parse(claim);
        System.out.println(parse);
        Long username = Long.parseLong(parse.getByPath("userId").toString());
        return username;
    }
    public List<Integer> getRole(){
        String authorization = httpServletRequest.getHeader("Authorization");
        Claims claim = JwtUtils.getClaimByToken(authorization, "community");
        JSON parse = JSONUtil.parse(claim);
        System.out.println(parse);
        System.out.println(parse.getByPath("role"));
        List<Integer> role = (List<Integer>) parse.getByPath("role");
        return role;
    }
    @GetMapping("/getOwnerComplainList")
    public Result getOwnerComplainList(Long residentialId,String name,Long userId){
        return Result.success(complainService.getOwnerComplainList(getPage(),residentialId,name,userId));
    }
    @PostMapping("/score")
    public Result scoreComplain(Complain complain){
        complainService.saveOrUpdate(complain);
        return Result.success();
    }
    @GetMapping("/total")
    public Long total(){
        return complainService.count();
    }
    @GetMapping("/getComplainTotal")
    public Object getComplainTotal(){
        return complainService.getComplainTotal();
    }

    @GetMapping("/getWeekComplainTotal")
    public Object getWeekComplainTotal(){
        return complainService.getWeekComplainTotal();
    }
}
